package com.wims.client;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

public class LoginPage extends PageWrapper {

	/** HTML element corresponding to this page */
	public static final String pageName = "loginScreen";

	// Fields of this page
	private Button sendButton;
	private TextBox nameField;

	/** Reference to the service. Initialized externally */
	private GreetingServiceAsync service;

	/** Reference to the next page. Obtained from the holder on first use */
	private ThreeColumnPage nextPage = null;

	public LoginPage(PageHolder holder, GreetingServiceAsync service) {
		super(holder);
		this.service = service;
	}

	@Override
	public String getPageName() {
		return pageName;
	}

	@Override
	protected void createUI() {
		sendButton = new Button("Submit");
		nameField = new TextBox();
		nameField.setText("GWT User"); // TODO remove default name

		// We can add style names to widgets
		sendButton.addStyleName("sendButton");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("nameFieldContainer").add(nameField);
		RootPanel.get("sendButtonContainer").add(sendButton);

		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);
		nameField.selectAll();

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		nameField.addKeyUpHandler(handler);
	}

	/**
	 * Send the name from the nameField to the server and wait for a response.
	 */
	private void sendNameToServer() {
		final String textToServer = nameField.getText();
		service.getPersonTypes(new AsyncCallback<ArrayList<String>>() {
			public void onFailure(Throwable caught) {
				holder.showMessage("Oops: " + caught);
			}

			public void onSuccess(ArrayList<String> result) {
				if (nextPage == null)
					nextPage = (ThreeColumnPage) holder
							.findPage(ThreeColumnPage.pageName);
				// assert (nextPage != null) : "Broken HTML, will die on NPE";
				nextPage.doLoginDone(textToServer, result);
			}
		});
	}

	/** Called by various pages when logout button is clicked */
	public void doLogout() {
		// TODO Invoke logout on the server. Some standard code?

		// Clean up user name/password/whatever fields
		nameField.setText("user name");

		// Make this page visible
		this.holder.setActivePage(pageName);
	}

	/** Handler for the sendButton and nameField */
	private class MyHandler implements ClickHandler, KeyUpHandler {
		/** Fired when the user clicks on the sendButton */
		public void onClick(ClickEvent event) {
			sendNameToServer();
		}

		/** Fired when the user types in the nameField */
		public void onKeyUp(KeyUpEvent event) {
			if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
				sendNameToServer();
			}
		}
	}
}
